package org.xnsg.app.controller;

import com.eova.aop.AopContext;
import com.eova.aop.MetaObjectIntercept;
import com.eova.model.*;
import com.eova.service.sm;
import com.eova.template.common.util.TemplateUtil;
import com.eova.vo.MenuConfig;
import com.eova.widget.WidgetManager;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import org.xnsg.app.core.bean.Result;

import java.util.*;

/**
 * Created by zhaopan on 2017/4/23.<br>
 *
 * @Description:
 */
public class SpecialController extends Controller {
    final Controller ctrl = this;
    protected MetaObjectIntercept intercept = null;
    private String errorInfo = "";
    private final Record record = new Record();

    public void list() {
        Map resultMap = new HashMap();
        String menuCode = this.getPara(0);
        Menu menu = Menu.dao.findByCode(menuCode);
        //resultMap.put("menu", menu);
        MenuConfig config = menu.getConfig();
        String objectCode = config.getObjectCode();
        //ArrayList objects = new ArrayList();
        //List objectCodes = config.getObjects();
        //Iterator user = objectCodes.iterator();

        //while(user.hasNext()) {
        //    String fields = (String)user.next();
        //    objects.add(MetaObject.dao.getByCode(fields));
        //}
        //this.setAttr("object", MetaObject.dao.getByCode(objectCode));
        //this.setAttr("objects", objects);
        //this.setAttr("config", config);
        //resultMap.put("configJson", menu.getStr("config"));
        //resultMap.put("isQuery", Boolean.valueOf(MetaObject.dao.isExistQuery(objectCode)));
        List fields = MetaField.dao.queryByObjectCode(objectCode);
        resultMap.put("fields", fields);

        User user1 = (User) this.getSessionAttr("user");
        List btnList = Button.dao.queryByMenuCode(menuCode, user1.getInt("rid").intValue(), 0);
        HashMap btnMap = new HashMap();
        Button b;
        Object list;
        for (Iterator var12 = btnList.iterator(); var12.hasNext(); ((List) list).add(b)) {
            b = (Button) var12.next();
            int group = b.getInt("group_num").intValue();
            list = (List) btnMap.get(Integer.valueOf(group));
            if (list == null) {
                list = new ArrayList();
                btnMap.put(Integer.valueOf(group), list);
            }
        }
        resultMap.put("btnMap", btnMap);

        this.renderJson(new Result(resultMap));
    }
    public void detail() {
        String userId = getPara("userId");
        User user = User.dao.getByLoginId(getPara("userId"));
        this.setSessionAttr("user", user);
        MetaObject object = this.buildFormData(false);
        Map resultMap = new HashMap();
        //resultMap.put("object", object);
       // resultMap.put("fields", object.getFields());
        // this.buildWidget(resultMap);
        //resultMap.put("stype", ControllerHelper.comboJson(null, "c_aj_wgsj", "stype", new Object[]{this.getSessionAttr("user")}));
        //resultMap.put("step", ControllerHelper.comboJson(null, "c_pt_user", "step", new Object[]{this.getSessionAttr("user")}));
        for (MetaField field : object.getFields()){
            resultMap.put(field.getStr("en"), String.valueOf(field.get("value")));
        }
        this.removeSessionAttr("user");
        this.renderJson(new Result(resultMap));
    }
    public MetaObject buildFormData(boolean isEdit) {
        String objectCode = this.getPara(0);
        String pkValue = this.getPara(1);
        MetaObject object = sm.meta.getMeta(objectCode);
        Record record = Db.use(object.getDs()).findById(object.getView(), object.getPk(), new Object[]{pkValue});
        MetaField ei;
        Object value;
        for(Iterator var7 = object.getFields().iterator(); var7.hasNext(); ei.put("value", value)) {
            ei = (MetaField)var7.next();
            String key = ei.getEn();
            value = record.get(key);
            if(value == null) {
                value = "";
            }
            if(!isEdit) {
                ei.set("is_disable", Boolean.valueOf(true));
            }
        }
        WidgetManager.buildRef(this, object);
        return object;
    }
    public void add() throws Exception {
        String objectCode = this.getPara(0);
        MetaObject object = sm.meta.getMeta(objectCode);
        Iterator var4 = object.getFields().iterator();

        while(var4.hasNext()) {
            MetaField ac = (MetaField)var4.next();
            ac.put("is_disable", Boolean.valueOf(false));
        }

        WidgetManager.buildRef(this, object);
        this.intercept = (MetaObjectIntercept) TemplateUtil.initIntercept(object.getBizIntercept());
        if(this.intercept != null) {
            AopContext ac1 = new AopContext(this.ctrl, object);
            this.intercept.addInit(ac1);
        }

        Map resultMap = new HashMap();
        //resultMap.put("object", object);
        resultMap.put("fields", object.getFields());
        this.renderJson(new Result(resultMap));
    }
    public void update() throws Exception {
        User user = User.dao.getByLoginId(getPara("userId"));
        this.setSessionAttr("user", user);
        MetaObject object = this.buildFormData(true);
        this.intercept = (MetaObjectIntercept)TemplateUtil.initIntercept(object.getBizIntercept());
        if(this.intercept != null) {
            AopContext ac = new AopContext(this.ctrl, object);
            this.intercept.updateInit(ac);
        }
        Map resultMap = new HashMap();
        //resultMap.put("object", object);
       // resultMap.put("fields", object.getFields());
        for (MetaField field : object.getFields()){
            resultMap.put(field.getStr("en"), String.valueOf(field.get("value")));
            resultMap.put(field.getStr("en")+"_qx", (boolean)field.get("is_update"));

        }
        this.removeSessionAttr("user");
        this.renderJson(new Result(resultMap));
    }
}
